import AntModal from "@/components/pt/dialog/AntModal";
import DeptTree from "../../../system/user/modules/AssessDeptTree.vue";
import AssessDetailsList from "../../assessdetailsinfo/indexAdd.vue";
import AssessDeptList from "../../bizassesstodept/index.vue";
import {
  getBizAssessInfo,
  addBizAssessInfo,
  updateBizAssessInfo,
  fabuBizAssessInfo,
  findMaxSort,
  getId,
} from "@/api/biz/bizAssessInfo";
import { listDeptTree } from "@/api/system/dept";
import {
  addBizAssessToDeptDetailsFile,
  delBizAssessToDeptDetailsFile,
  listBizAssessToDeptDetailsFile,
} from "@/api/biz/bizAssessToDeptDetailsFile";
import FileUpload from "@/components/pt/fileupload/IndexList";
export default {
  name: "CreateForm",
  props: {},
  components: {
    AntModal,
    DeptTree,
    AssessDetailsList,
    AssessDeptList,
    FileUpload,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      adjustSize: false,
      formTitle: "添加考核信息",
      deptOptions: [],
      expandedKeys: [],
      checkKeys: [],
      setDataOptionInfo: [],
      clickDeptNode: [],
      fileList: [],
      // 表单参数
      form: { deptIds: [] },

      rules: {
        title: [
          { required: true, message: "考核标题不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "截止时间不能为空", trigger: "change" },
        ],
      },
    };
  },
  filters: {},
  created() {
    this.getTreeselect();
    this.handlerGetId();
  },
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    handlelistBizAssessToDeptDetailsFile(bizAssessInfoId) {
      listBizAssessToDeptDetailsFile({
        assessDeptDetailsId: bizAssessInfoId.id,
      }).then((response) => {
        this.fileList = response.data.list.map((item) => {
          return {
            name: item.fileName,
            url: item.fileUrl,
            uid: item.id,
            status: "done",
          };
        });
      });
    },
    handlerGetId() {
      getId().then((response) => {
        this.assessInfoId = response.data;
      });
    },
    uploadCompleteFile(item, items) {
      console.log(("item", item, items));
      addBizAssessToDeptDetailsFile({
        assessDeptDetailsId: this.assessInfoId,
        fileName: item.fileName,
        fileUrl: item.url,
      }).then((response) => {
        this.$message.success("上传成功", 3);
        this.fileList = items;
      });
    },
    handlerRemoveFile(file) {
      console.log("file", file);
      delBizAssessToDeptDetailsFile({
        fileUrl: file.url,
        fileName: file.name,
      }).then((response) => {
        this.$message.success("删除成功", 3);
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      listDeptTree("0", 99).then((response) => {
        this.deptOptions = response.data;
        this.getExpandedKeys(this.deptOptions, 99);
      });
    },
    getExpandedKeys(nodes, expandLevel) {
      if (expandLevel > 1) {
        // 最后一层不展开
        if (nodes != null) {
          nodes.forEach((node) => {
            this.expandedKeys.push(node.id);
            expandLevel = expandLevel - 1;
            return this.getExpandedKeys(node.children, expandLevel);
          });
        }
      }
    },
    onClose() {
      // this.open = false;
      // this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.reset();
      this.$emit("ok");
      // console.log(this.open);
      // this.$message.success("保存草稿成功！", 3);
    },
    saveCaoGao() {
      // this.open = false;
      // this.reset();
      // this.$emit("ok");
      // console.log(this.open);
      this.$message.success("保存草稿成功！", 3);
    },
    closeAddDetails() {
      this.open = false;
    }, // this.open = false;
    handlerGetcheckedKeys(value) {
      console.log("deptIds", value);
      this.form.deptIds = value;
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        title: undefined,
        endTime: undefined,
        type: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "添加考核信息";
      });
    },
    handleDetailsAdd(row) {
      this.open = true;
      /** 获取最大编号 */
      this.form = row;
      this.formTitle = "添加考核内容详情";
      // this.$nextTick(() => this.$refs.addDetailsList.getList(row.id));
    },

    handleDetailsListAdd(row) {
      this.open = true;
      /** 获取最大编号 */
      this.form = row;
      this.formTitle = "考核部门列表";
      // this.$nextTick(() => this.$refs.addDetailsList.getList(row.id));
    },

    handleFaBu() {
      const that = this;
      that.$confirm({
        title: "确认发布考核，发布后无法修改与撤回?",
        onOk() {
          const saveForm = JSON.parse(JSON.stringify(that.form));
          saveForm.status = 1;
          return fabuBizAssessInfo(saveForm).then((response) => {
            that.$message.success("发布成功", 3);
            that.open = false;
            that.$emit("ok");
          });
        },
        onCancel() {},
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizAssessInfoId = row.id;
      this.checkKeys = JSON.parse(row.remark);
      console.log("this.checkKeys", this.checkKeys);
      getBizAssessInfo(bizAssessInfoId).then((response) => {
        this.form = response.data;
        this.formTitle = "修改考核信息";
        this.spinning = !this.spinning;
        this.form.deptIds = this.checkKeys;
        this.handlelistBizAssessToDeptDetailsFile(response.data);
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      if (this.form.deptIds && this.form.deptIds.length > 0) {
        this.$refs.form.validate((valid) => {
          console.log("this.form.deptIds", this.form.deptIds);
          if (valid) {
            if (this.form.id !== undefined) {
              this.form.type = "日常考核";

              this.form.remark = JSON.stringify(this.form.deptIds);
              const saveForm = JSON.parse(JSON.stringify(this.form));
              updateBizAssessInfo(saveForm).then((response) => {
                this.$message.success("修改成功", 3);
                this.open = false;
                this.$emit("ok");
              });
            } else {
              this.form.id = this.assessInfoId;
              this.form.status = 0;
              this.form.type = "日常考核";
              this.form.remark = JSON.stringify(this.form.deptIds);
              const saveForm = JSON.parse(JSON.stringify(this.form));
              addBizAssessInfo(saveForm).then((response) => {
                this.$message.success("新增成功", 3);
                this.open = false;
                this.fileList = [];
                this.$emit("ok");
              });
            }
          } else {
            // return false;
            console.log("error submit!!", valid);
          }
        });
      } else {
        this.$message.error("请选择考核部门", 3);
        return false;
      }
    },
    back() {
      const index = "/biz/assessinfo/index";
      this.$router.push(index);
    },
  },
};
